<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$_SESSION['forward_url']='jy_ailiao/index/';
	if(!$_SESSION['openid']){
		$_SESSION['islogin']=1;
		$return['ischecklogin']=$this->model('member')->get_member();
	}
	if(!$_SESSION['uid']){
		$return['ischecklogin']=$this->model('member')->get_member();
	}
	if($_SESSION['islogin'] && $_SESSION['uid']){
		$this->model('member')->checkmyinfo();
	}
	$return['setting']=$return['pluginset'];
	$title=$return['pluginset']['share_title']?:'婚恋相亲';
	$desc=$return['pluginset']['share_desc'];
	$logo=$return['pluginset']['share_image'];
	if($_SESSION['uid']){
		$return['member']=$this->model('member')->get_member($_SESSION['uid']);
		$_SESSION['sex']=$return['member']['sex'];
		$return['member']['credit1passtime']=date('Y年m月d日',time()+7*24*3600);
	}
	if($_RQ['type']==6 && !$return['member']['level_id']){
		exi('推荐页面需VIP才能浏览！','error','/member.buyvip/');
	}
	$params=array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'onepluginname'=>'jy_ailiao');
	$params['parentid']=intval($_RQ['id']);
	$category=pdo_getall('jy_weishop_goods_group',$params,'','id','paixu desc');
	$return['category']=$category;
	$return['list']=array();
	$return['pcd']=$this->session('pcd')?:array('city'=>'');
	$return['params']['type']=$_RQ['type']?:3;
	$return['params']['keyword']='';
	$lat_lng=$this->session('lat_lng');
	$return['params']['lat']=$lat_lng['lat'];
	$return['params']['lng']=$lat_lng['lat'];
	$_SESSION['tantime']=$_SESSION['tantime']?:0;
	$return['post']=array('tomid'=>0,'jy'=>'');
	$return['time']=time();
	if($_SESSION['tantime']<time()-1800){
		if($this->setting['open_tanimg']==2 || $this->setting['open_tanimg']==3){
			$return['tanfans']=$this->model('member')->get_tanfans();
			foreach($return['tanfans'] as &$tv){
				$tv['selected']=1;
			}
		}else{
			
		}
		$_SESSION['tantime']=time();
	}else{
		$return['noshowtanimg']=true;
	}
	$return['sex']=$this->sex;
	$return['sex'][0]='不限';
	$return['xuelis']=$this->xuelis2;
	$return['shourus']=$this->shourus;
	$return['years']=$this->years;
	$return['tixing']=$this->tixing;
	$return['pcd']=$this->session('pcd')?:array('city'=>'');
	$return['siimg']=$this->plugin_setting('siimg');
	$return['ping']=$this->plugin_setting('ping');
	$return['sivideo']=$this->plugin_setting('sivideo');
	$sharelink='jy_ailiao/index//ismerch=1&merchid=0&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$title?:'婚恋相亲',
			'desc'=>$desc?:'婚恋相亲，成就天下姻缘！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($logo),
	);
	set_html('title',$title);
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getitem'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>0,
	);
	if($_SESSION['sex']==2){
		$params['sex']=1;
	}else{
		$params['sex']=2;
	}	
	if(trim($_RQ['keyword'])){
		$params['nickname like']='%'.trim($_RQ['keyword']).'%';
	}
	$lastid=$this->session('lastid');
	if($lastid){
		$params['id <']=$lastid;
	}
	if($this->setting['no_memberimg']){
		$params['images <>']='';
		$params['avatar <>']='';
	}
	if(intval($_RQ['lat'])){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng');
	//print_r($lat_lng);exit;
	$orderby='paixu desc, star desc, id desc ';
	$item=pdo_get('jy_ailiao_member',$params,$fields,$orderby);
	if($item['id']){
		$this->session('lastid',$item['id']);
	}else{
		$this->session('lastid','');
		unset($params['id <']);
		$item=pdo_get('jy_ailiao_member',$params,$fields,$orderby);
		if($item['id']){
			$this->session('lastid',$item['id']);
		}
	}
	$item['avatar']=tomedia($item['avatar']);
	$item['images']=json_decode($item['images'],true);
	$item['birth']=json_decode($item['birth'],true);
	$item['jiguan']=json_decode($item['jiguan'],true);
	foreach($item['images'] as &$img){
		$img=tomedia($img);
	}
	$item['image']=$item['images'][0]?:$item['avatar'];
	$item['sextitle']=$this->sex[$item['sex']];
	$item['height']=$this->heights[$item['height']];
	$item['picsnum']=count($item['images']);
	$item['lat_lng']=json_decode($item['lat_lng'],true);
	if($item['lat_lng']){
		$item['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$item['lat_lng']['lng'],$item['lat_lng']['lat']);
		$item['juli']=price($item['juli']/1000);
	}
	$current_year=date('Y');
	$item['age'] = $item['birth']['year']?$current_year - $item['birth']['year'] - 1:'**';
	$minzus=$this->model('model')->minzus();
	$zhiweis=$this->model('model')->zhiweis();
	$item['zhiweititle']=$zhiweis[$item['zhiwei']];
	$myinfo=array(
		array('id'=>$item['minzu'],'title'=>$minzus[$item['minzu']]),
		array('id'=>$item['jiguan']['province'],'title'=>$item['jiguan']['province']?'籍贯:'.$item['jiguan']['province']:'籍贯:***'),
		array('id'=>$item['xueli'],'title'=>$this->xuelis[$item['xueli']]),
		array('id'=>$item['zhiwei'],'title'=>$zhiweis[$item['zhiwei']]),
		array('id'=>$item['isjiehun'],'title'=>$this->jiehuns[$item['isjiehun']]),
		array('id'=>$item['shouru'],'title'=>'薪资'.$this->shourus[$item['shouru']]),
		array('id'=>$item['youboy'],'title'=>$this->youboy[$item['youboy']]),
		array('id'=>$item['wanboy'],'title'=>$this->wanboy[$item['wanboy']]),
		array('id'=>$item['goufang'],'title'=>$this->goufang[$item['goufang']]),
		array('id'=>$item['gouche'],'title'=>$this->gouche[$item['gouche']]),
		array('id'=>$item['weight'],'title'=>'体重'.$this->weights[$item['weight']]),
		array('id'=>$item['height'],'title'=>'身高'.$this->heights[$item['height']]),
		array('id'=>$item['tixing'],'title'=>'体型'.$this->tixing[$item['tixing']]),
		array('id'=>$item['xiyan'],'title'=>$this->xiyan[$item['xiyan']]),
		array('id'=>$item['hejiu'],'title'=>$this->hejiu[$item['hejiu']]),
		array('id'=>$item['jh_time'],'title'=>$this->jh_time[$item['jh_time']]),
		array('id'=>$item['xingzuo'],'title'=>$this->xingzuo[$item['xingzuo']]),
	);
	$item['myinfo']=$myinfo;
	$toitem=pdo_get('jy_ailiao_tomember',array('uniacid'=>$_SESSION['uniacid'],'memberid'=>$item['mid']));
	if($toitem['id']){
		$toitem['birth']=json_decode($toitem['birth'],true);
		$toitem['jiguan']=json_decode($toitem['jiguan'],true);
		$toinfo=array(
			array('id'=>$toitem['minzu'],'title'=>$minzus[$toitem['minzu']]),
			array('id'=>$toitem['agekey'],'title'=>$this->years[$toitem['agekey']]),
			array('id'=>$toitem['jiguan']['province'],'title'=>$toitem['jiguan']['province']?'籍贯:'.$toitem['jiguan']['province']:'籍贯:***'),
			array('id'=>$toitem['xueli'],'title'=>$this->xuelis[$toitem['xueli']]),
			array('id'=>$toitem['zhiwei'],'title'=>$zhiweis[$toitem['zhiwei']]),
			array('id'=>$toitem['isjiehun'],'title'=>$this->jiehuns[$toitem['isjiehun']]),
			array('id'=>$toitem['shouru'],'title'=>'薪资'.$this->shourus[$toitem['shouru']]),
			array('id'=>$toitem['youboy'],'title'=>$this->youboy[$toitem['youboy']]),
			array('id'=>$toitem['wanboy'],'title'=>$this->wanboy[$toitem['wanboy']]),
			array('id'=>$toitem['goufang'],'title'=>$this->goufang[$toitem['goufang']]),
			array('id'=>$toitem['gouche'],'title'=>$this->gouche[$toitem['gouche']]),
			array('id'=>$toitem['weight'],'title'=>'体重'.$this->weights[$toitem['weight']]),
			array('id'=>$toitem['height'],'title'=>'身高'.$this->heights[$toitem['height']]),
			array('id'=>$toitem['tixing'],'title'=>'体型'.$this->tixing[$toitem['tixing']]),
			array('id'=>$toitem['xiyan'],'title'=>$this->xiyan[$toitem['xiyan']]),
			array('id'=>$toitem['hejiu'],'title'=>$this->hejiu[$toitem['hejiu']]),
			array('id'=>$toitem['jh_time'],'title'=>$this->jh_time[$toitem['jh_time']]),
			array('id'=>$toitem['xingzuo'],'title'=>$this->xingzuo[$toitem['xingzuo']]),
		);
	}
	$item['toinfo']=$toinfo;
	$isadd=pdo_get('jy_ailiao_likes',array('mid'=>$_SESSION['uid'],'tomid'=>$item['mid']));
	if($isadd['id']){
		$item['islike']=1;
	}else{
		$item['islike']=0;
	}
	$item['newsnum'] = pdo_count('jy_ailiao_newslog',array('simi'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
	$item['sivideonum']=pdo_count('jy_ailiao_newslog',array('simi'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
	$siimgorder=pdo_get('jy_ailiao_siimgorder',array('tomid'=>$item['mid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$item['isbuysiimg']=$siimgorder['status'];
	if($item['isbuysiimg']){
		$minzus=$this->model('siimg')->add_siimlog($item['mid']);
	}
	$siimgorder=pdo_get('jy_ailiao_sivideoorder',array('tomid'=>$item['mid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$item['isbuysivideo']=$siimgorder['status'];
	$member=$this->model('member')->get_member();
	if($member['cansimi']){
		$item['isbuysiimg']=$item['isbuysivideo']=1;
	}
	$item['wechatnew']=$item['wechat']?:'********';
	if($this->setting['wechat_hong'] && $item['worker_id']){
		$item['wechatnew']=pdo_getcolumn('jy_ailiao_worker',array('id'=>$item['worker_id'],'uniacid'=>$_SESSION['uniacid']),'wechat');
	}
	$item['liao_lookmsg']=$this->model('member')->looklog($item['mid']);
	exi($item);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>0,
	);
	if($_RQ['sex']){
		$params['sex']=intval($_RQ['sex']);
	}	
	if(trim($_RQ['keyword'])){
		$params['nickname like']='%'.trim($_RQ['keyword']).'%';
	}
	if(intval($_RQ['lat'])){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		//print_r($lat_lng);exit;
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$orderby='paixu desc, star desc, id desc ';
	$list=pdo_getslice('jy_ailiao_member',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if(empty($list)){
		exi('none');
	}else{
		foreach($list as $k=>&$itemv){
			$itemv['avatar']=tomedia($itemv['avatar']);
			$itemv['images']=json_decode($itemv['images'],true);
			foreach($itemv['images'] as &$img){
				$img=tomedia($img);
			}
			$itemv['image']=$itemv['images'][0]?:$itemv['avatar'];
			$itemv['sextitle']=$this->sex[$itemv['sex']];
			$itemv['lat_lng']=json_decode($itemv['lat_lng'],true);
			if($itemv['lat_lng']){
				$itemv['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$itemv['lat_lng']['lng'],$itemv['lat_lng']['lat']);
				$itemv['juli']=price($itemv['juli']/1000);
			}
		}
	}
	exi($list);
}
// 发送约会瓶
if($_RQ['op']=='sendping'){
	$postdata=get_postdata();
	if(!$postdata['desc']){
		exi('内容不能为空！','error');
	}
	if(!$postdata['tomid']){
		exi('请选择发送对象，或者确认该对象已绑定会员！','error');
	}
	$this->model('model')->check_text($postdata['desc']);
	if($_SESSION['wxapp_id'] && $_SESSION['container']=='wxapp'){
		$result=model('core/wxapp_api')->msgSecCheck($postdata['desc']);
		if($result['errcode']){
			exi('内容可能不合法！','error');
		}
	}
	$member=$this->model('member')->get_member();
	if($member['pingnum']<=0){
		exi('您当前的约会瓶数量为'.$member['pingnum'].'！不能发送约会！','error','/ping.buyping/');
	}
	$data=array(
		'mid'=>$_SESSION['uid'],
		'uniacid'=>$_SESSION['uniacid'],
		'tomid'=>$postdata['tomid'],
		'desc'=>$postdata['desc'],
		'video'=>$postdata['video'],
		'images'=>ijson_encode($postdata['images']),
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_ailiao_pinglog',$data);
	$this->update('member',array('pingnum -='=>1),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('发送成功！');
}
if($_RQ['op']=='uppcd'){
	$post=get_postdata();
	if(intval($post['lat'])){
		$lat_lng=array('lat'=>$post['lat'],'lng'=>$post['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$this->session('pcd',$post);
	exi('更新成功！');
}
if($_RQ['op']=='uplat_lng'){
	$post=get_postdata();
	if(intval($post['lat'])){
		$lat_lng=array('lat'=>$post['lat'],'lng'=>$post['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	exi('更新成功！');
}
?>